package com.example.zhyq.repair

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.base.entity.RepairBeanItem
import com.example.base.mvvm.BaseActivity
import com.example.zhyq.R
import com.example.zhyq.databinding.ActivityAddRepairBinding
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/AddRepairActivity")
class AddRepairActivity : BaseActivity<ActivityAddRepairBinding,RepairViewModel>() {

    var area = "A区停车场"
    val repairImgAdapter by lazy { RepairImgAdapter() }
    override fun initView() {

        binding.headRepairAdd.headerBack.setOnClickListener {
            finish()
        }
        binding.headRepairAdd.headerTitle.setText("添加维修")
        binding.headRepairAdd.headerAdd.apply {
            setOnClickListener {
                val beanItem = RepairBeanItem()
                val phone = binding.repairaddPhone.text.toString()
                val remark = binding.repairaddRemark.text.toString()
                val user = binding.repairaddUser.text.toString()
                val currentTime = System.currentTimeMillis()
                beanItem.contactMethod = phone
                beanItem.faultDescription = remark
                beanItem.faultPhotos = ""
                beanItem.id = null
                beanItem.orderNumber = "WX${vm.getDateTimeString(currentTime)}"
                beanItem.processingStatus = "待处理"
                beanItem.repairLocation = area
                beanItem.repairName = user
                beanItem.repairTime= vm.getDataTime(currentTime)
                beanItem.userId = 890
                vm.addRepair(beanItem)
            }
        }
        val arr = arrayOf("A区停车场","B区停车场","C区停车场")
        val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, arr)
        binding.repairaddLocarion.adapter =adapter
        binding.repairaddLocarion.onItemSelectedListener=object :AdapterView.OnItemSelectedListener{
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                area=arr[p2]
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }

        }

        //点击添加图片
        binding.repairaddImg.setOnClickListener {
            if (repairImgAdapter.data.size<4){
                PictureSelector.create(this,PictureSelector.SELECT_REQUEST_CODE).selectPicture()
            }else {
                ToastUtils.showLong("图片已达上限")
            }
        }

        binding.repairaddRv.apply {
           this.adapter = repairImgAdapter
            layoutManager = LinearLayoutManager(this@AddRepairActivity,RecyclerView.HORIZONTAL,false)
        }
    }

    override fun initData() {
        vm.addRepairSuc.observe(this) {
            ToastUtils.showLong("添加成功")
            finish()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode==PictureSelector.SELECT_REQUEST_CODE && resultCode== RESULT_OK) {
            val bean = data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            repairImgAdapter.data.add(bean!!.path)
            repairImgAdapter.notifyDataSetChanged()
        }
    }
}